package com.example.hw;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


import android.os.Bundle;
import android.provider.Contacts.Organizations;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.support.v4.widget.ScrollerCompat;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.Scroller;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class ActDrink extends Activity {
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_drink);
		InitialComponet();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_drink, menu);
		return true;
	}
	
	Button btnBackToMain2 = null;
	Button[] btnSetDrinks	  = null;
	Button btnAddDrink = null;
	
	TableLayout tableDrinks = null;
	NumberPicker np			=null;

	private void InitialComponet() {
		
		btnBackToMain2 = (Button)findViewById(R.id.btnBackToMain2);
		btnBackToMain2.setOnClickListener(btn_goBackToMain);
		
		btnAddDrink = (Button)findViewById(R.id.btnAddDrink);
		btnAddDrink.setOnClickListener(btnAddDrink_click);
		
		tableDrinks = (TableLayout)findViewById(R.id.tableDrinks);		
		showAllDrinks();
	}
	
	Dialog dlgAdd;
	
	OnClickListener btnAddDrink_click = new OnClickListener() {
		public void onClick(View v) {
			Builder build=new Builder(ActDrink.this);
			//設定標頭
			build.setTitle("新增飲料");
			//取得Layout
			build.setView(drinkAdd());
			dlgAdd = build.create();
			dlgAdd.show();
		}
	};
	
	public LinearLayout drinkAdd(){	
	
		LinearLayout ll = new LinearLayout(ActDrink.this);
		//為了方便取得這個Layout的ID　
		ll.setId(1111);
		ll.setOrientation(1);
		ll.setLayoutParams(new TableRow.LayoutParams
				(TableRow.LayoutParams.MATCH_PARENT, 
				 TableRow.LayoutParams.MATCH_PARENT));
		
		LinearLayout llup = new LinearLayout(ActDrink.this);
		llup.setOrientation(1);
		llup.setLayoutParams(new TableRow.LayoutParams
				(TableRow.LayoutParams.MATCH_PARENT, 
				 TableRow.LayoutParams.MATCH_PARENT));
		
		LinearLayout lldown = new LinearLayout(ActDrink.this);
		lldown.setOrientation(0);
		lldown.setLayoutParams(new TableRow.LayoutParams
				(TableRow.LayoutParams.MATCH_PARENT, 
				 TableRow.LayoutParams.MATCH_PARENT));
	
		llup.addView(txtDrinkName());
		llup.addView(editDrinkName());
		llup.addView(txtDrinkPrice());
		llup.addView(pricePick());
		
		lldown.addView(btnAddDrink());
		lldown.addView(btnCancelAddDrink());
		
		ll.addView(llup);
		ll.addView(lldown);
		return ll;
	}
	
	
	private View btnCancelAddDrink() {
		Button no = new Button(ActDrink.this);
		no.setText("取消");
		no.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
				LayoutParams.WRAP_CONTENT,1.0f));
		no.setOnClickListener(btnCancelAdd);
		return no;
	}
	OnClickListener btnCancelAdd = new OnClickListener() {
		@Override
		public void onClick(View v) {
			//關閉dlg
			dlgAdd.cancel();
			Toast.makeText(ActDrink.this, "取消新增", Toast.LENGTH_SHORT).show();
		}
	};

	private View btnAddDrink() {
		Button yes = new Button(ActDrink.this);
		yes.setText("新增");
		yes.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
				LayoutParams.WRAP_CONTENT,1.0f));
		yes.setOnClickListener(add_click);

		return yes;
	}
	
	OnClickListener add_click = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String drinkName = addDrinkName.getText().toString();
			int price = np.getValue();
			
			ShopFactory sf = new ShopFactory();
			
			if(drinkName.equals("")|| drinkName==""){
				//價格沒變動，返回
				Toast.makeText(ActDrink.this, "沒有飲料名", Toast.LENGTH_SHORT).show();
				return;
			}else{
				DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd k:mm:ss");
				Date addDate = new Date();
				
				sf.goInsertProduct(ActDrink.this, drinkName, price, timeFormat.format(addDate));
				//刪除目前飲料的View
				tableDrinks.removeAllViews();
				//更新新的View
				showAllDrinks();
				dlgAdd.cancel();
				
				Toast.makeText(ActDrink.this, "飲料上架成功", Toast.LENGTH_LONG).show();
			}
		}
	};

	private View editDrinkPrice() {
		EditText drinkPrice = new EditText(ActDrink.this);
		drinkPrice.setId(3333);
		drinkPrice.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
				LayoutParams.WRAP_CONTENT,1.0f));
		
		return drinkPrice;
	}

	private View txtDrinkPrice() {
		TextView drinkPrice = new TextView(ActDrink.this);
		drinkPrice.setText("價格");
		drinkPrice.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
				LayoutParams.WRAP_CONTENT,1.0f));
		
		return drinkPrice;
	}
	
	//要取得飲料名字是啥
	EditText addDrinkName = null;
	private View editDrinkName() {
		addDrinkName = new EditText(ActDrink.this);
		//取得飲料
		addDrinkName.setId(2222);
		addDrinkName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
				LayoutParams.WRAP_CONTENT,1.0f));
		return addDrinkName;
	}

	private View txtDrinkName() {
		TextView drinkName = new TextView(ActDrink.this);
		drinkName.setText("飲料名");
		drinkName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
				LayoutParams.WRAP_CONTENT,1.0f));
		
		return drinkName;
	}

	int product_id	=0;
	String drinkName = null;
	int price		= 0;
	String date		= null;
	Dialog dlg 		= null;
	
	OnClickListener fixDrinks = new OnClickListener() {
		public void onClick(View v) {
			Builder build=new Builder(ActDrink.this);
			TableRow row = (TableRow) tableDrinks.getChildAt(v.getId());
			setDrink(row);
			//設定標頭
			build.setTitle(((TextView)row.getChildAt(1)).getText());
			//取得Layout
			build.setView(drinkll(v.getId()));
			dlg = build.create();
			dlg.show();
		}
	};
	//取得目前的飲料訊息
	protected void setDrink(TableRow row) {
		DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd k:mm:ss");
		Date updateDate = new Date();
		
		product_id = Integer.parseInt(((TextView)row.getChildAt(0)).getText().toString());
		drinkName  = ((TextView)row.getChildAt(1)).getText().toString();
		price	   = Integer.parseInt(((TextView)row.getChildAt(2)).getText().toString());
		date	   = timeFormat.format((updateDate));
	}
	
	
	//設定目前的Layout
	public LinearLayout drinkll(int id){	
		TableRow row = (TableRow) tableDrinks.getChildAt(id);
		
		LinearLayout ll = new LinearLayout(ActDrink.this);
		ll.setOrientation(1);
		ll.setLayoutParams(new TableRow.LayoutParams
				(TableRow.LayoutParams.MATCH_PARENT, 
				 TableRow.LayoutParams.MATCH_PARENT));
		
		LinearLayout llup = new LinearLayout(ActDrink.this);
		llup.setOrientation(1);
		llup.setLayoutParams(new TableRow.LayoutParams
				(TableRow.LayoutParams.MATCH_PARENT, 
				 TableRow.LayoutParams.MATCH_PARENT));
		
		LinearLayout lldown = new LinearLayout(ActDrink.this);
		lldown.setOrientation(0);
		lldown.setLayoutParams(new TableRow.LayoutParams
				(TableRow.LayoutParams.MATCH_PARENT, 
				 TableRow.LayoutParams.MATCH_PARENT));
	
		int price = Integer.parseInt(((TextView)row.getChildAt(2)).getText().toString());	
		
		llup.addView(pricePick(price));
		lldown.addView(btnChangeDrinkPrice());
		lldown.addView(btnCancelDrinkPrice());
		
		ll.addView(llup);
		ll.addView(lldown);
		return ll;
	}
	

	public Button btnChangeDrinkPrice(){
		
		Button yes = new Button(ActDrink.this);
		yes.setText("修改價格");
		yes.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
				LayoutParams.WRAP_CONTENT,1.0f));
		yes.setOnClickListener(btnUpdatePrice);
		
		return yes;
	}
	
	OnClickListener btnUpdatePrice = new OnClickListener() {
		@Override
		public void onClick(View v) {
			int newPrice = np.getValue();
			if(newPrice == price){
				//價格沒變動，返回
				dlg.cancel();
				Toast.makeText(ActDrink.this, "價格無變動", Toast.LENGTH_SHORT).show();
				return;
			}else{
				ShopFactory sf = new ShopFactory();
				sf.goUpdateProduct(ActDrink.this, product_id, drinkName, newPrice, date);
				//刪除目前飲料的View
				tableDrinks.removeAllViews();
				//更新新的View
				showAllDrinks();
				dlg.cancel();
				Toast.makeText(ActDrink.this, "價格修改成功", Toast.LENGTH_SHORT).show();
			}
		}
	};
	
	
	public Button btnCancelDrinkPrice(){
		
		Button no = new Button(ActDrink.this);
		no.setText("取消");
		no.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
				LayoutParams.WRAP_CONTENT,1.0f));
		no.setOnClickListener(btnCancelPrice);
		return no;
	}
	
	OnClickListener btnCancelPrice = new OnClickListener() {
		@Override
		public void onClick(View v) {
			//關閉dlg
			dlg.cancel();
			Toast.makeText(ActDrink.this, "取消", Toast.LENGTH_SHORT).show();
		}
	};
	
	public NumberPicker pricePick(int price){
		np = new NumberPicker(ActDrink.this);	
		np.setId(10);
		np.setMaxValue(100);  
		np.setMinValue(10);    
		np.setValue(price); 
		np.setOnValueChangedListener(numChange);
		return np;
	}
	
	public NumberPicker pricePick(){
		np = new NumberPicker(ActDrink.this);	
		np.setId(10);
		np.setMaxValue(100);  
		np.setMinValue(10);    
		np.setValue(10); 
		np.setOnValueChangedListener(numChange);
		return np;
	}
	
	OnValueChangeListener numChange = new OnValueChangeListener (){
		@Override
		public void onValueChange(NumberPicker np, int oldValue, int newValue) {
			 System.out.println("新價格="+newValue);
		}
	};

	protected int getID(String product_id) {
		System.out.println("ID"+product_id);
		return Integer.parseInt(product_id);
	}
	
	private void showAllDrinks() {
		ShopFactory sf = new ShopFactory();
		String data[][] = sf.goQueryProducts(ActDrink.this);
		btnSetDrinks = new Button[data.length];
		
		tableDrinks.setStretchAllColumns(true);

		for(int i = 0 ; i < data.length ; i ++){
			TableRow tr = new TableRow(this);
			
			btnSetDrinks[i] = new Button(this);
			btnSetDrinks[i].setText("修改");
			btnSetDrinks[i].setOnClickListener(fixDrinks);
			btnSetDrinks[i].setId(i);
			tr.addView(getDrinkProductId(data[i][0]));
			tr.addView(getDrinkName(data[i][1]));
			tr.addView(getPrice(data[i][2]));
			tr.addView(getDate(data[i][3]));
			tr.addView(btnSetDrinks[i]);
			//System.out.println("ID="+data[i][0]+" date="+data[i][1]);
			tableDrinks.addView(tr, new TableLayout.LayoutParams
					(TableLayout.LayoutParams.WRAP_CONTENT,
					 TableLayout.LayoutParams.WRAP_CONTENT));
			
		}
	}
	
	
	

	private TextView getDate(String date) {
		TextView row4 = new TextView(this);
		row4.setLayoutParams(new TableRow.LayoutParams
		(TableRow.LayoutParams.WRAP_CONTENT, 
		 TableRow.LayoutParams.WRAP_CONTENT));
		row4.setGravity(Gravity.CENTER);
		row4.setText(date);
		return row4;
	}

	private TextView getPrice(String price) {
		TextView row3 = new TextView(this);
		row3.setLayoutParams(new TableRow.LayoutParams
		(TableRow.LayoutParams.WRAP_CONTENT, 
		 TableRow.LayoutParams.WRAP_CONTENT));
		row3.setGravity(Gravity.CENTER);
		row3.setText(price);
		return row3;
	}

	private TextView getDrinkName(String drinkName) {
		TextView row2 = new TextView(this);
		row2.setLayoutParams(new TableRow.LayoutParams
		(TableRow.LayoutParams.WRAP_CONTENT, 
		 TableRow.LayoutParams.WRAP_CONTENT));
		row2.setGravity(Gravity.CENTER);
		row2.setText(drinkName);
		return row2;
	}

	public TextView getDrinkProductId(String productId){
		TextView row1 = new TextView(this);
		row1.setLayoutParams(new TableRow.LayoutParams
		(TableRow.LayoutParams.WRAP_CONTENT, 
		 TableRow.LayoutParams.WRAP_CONTENT));
		row1.setGravity(Gravity.CENTER);
		row1.setText(productId);
		return row1;
	}

	OnClickListener btn_goBackToMain = new OnClickListener() {
		public void onClick(View v) {
			goFinish();
		}
	};
	
	protected void goFinish() {
		//返回上一個Class
		ActDrink.this.finish();
		Intent intent = new Intent(this, ActMain_Hw.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
	}
	

}
